package com.itheima.controller;

import com.itheima.anno.AccessLimit;
import com.itheima.pojo.Result;
import com.itheima.pojo.Cart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shoppingCart")
public class Controller {

    @Autowired
    private ShoppingCartService service;

    @AccessLimit
    @PostMapping("/add/{userId}")
    public Result add(@RequestBody Cart cart, @PathVariable("userId") Long Id) {
        //1.判断参数是否合法
        if (cart == null) {
            return new Result(0, "参数非法...");
        }
        //1.1.把用户输入的id放到对象当中,
        cart.setUserId(Id);
        //1.2.调用Servce层来实现整个业务代码
        Result result = service.add(cart);
        return result;
    }

    @AccessLimit
    @GetMapping("/list/{userId}")
    public Result findAllById(@PathVariable("userId") Long Id) {
        //检查参数
        if (Id == null) {
            return new Result(0, "id为空,参数异常!");
        }
        //调用方法进行查询,并返回结果
        return service.findById(Id);
    }

    /**
     * 清空用户的购物车
     * @param Id
     * @return
     */
    @AccessLimit
    @GetMapping("/clean/{userId}")
    public Result clean(@PathVariable("userId") Long Id) {
        //检查参数是否异常
        if (Id == 0) {
            return new Result(0, "参数异常!");
        }
        return service.clean(Id);
    }
}
